package com.ugrow.ugrowcommunity.activity.courseselection.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.courseselection.bean.Data;
import com.ugrow.ugrowcommunity.activity.courseselection.data.Teachers;
import com.ugrow.ugrowcommunity.databinding.CourseSelectionItemBinding;
import com.ugrow.ugrowcommunity.databinding.CourseTchItemBinding;

import java.util.List;

public class CourseTchAdapter extends RecyclerView.Adapter<CourseTchAdapter.Holder> {

    private CourseTchItemBinding courseItemBinding;
    private int index = -1;
    private Context context;
    private List<Teachers> list;
    public CourseTchAdapter(Context context, List<Teachers> list){
        this.context = context;
        this.list = list;
        Log.d("TAG",String.valueOf(list.size()));

    }

    public int getSelectTch() {
        return index;
    }

    @NonNull
    @Override
    public CourseTchAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(context).inflate(R.layout.course_tch_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull CourseTchAdapter.Holder holder, @SuppressLint("RecyclerView") int position) {
        String woman = "女";
        holder.itemBinding.tchName.setText(list.get(position).getTchName());
        holder.itemBinding.tchSex.setText(list.get(position).getTchSex());
        holder.itemBinding.tchAge.setText(String.valueOf(list.get(position).getTchAge()));
        holder.itemBinding.tchTitle.setText(list.get(position).getTchTitle());
        holder.itemBinding.tchEducation.setText(list.get(position).getTchEducation());
        holder.itemBinding.tchSystem.setText(list.get(position).getTchSystem());
        holder.itemBinding.tchPolitical.setText(list.get(position).getTchPolitical());
        holder.itemBinding.tchNation.setText(list.get(position).getTchNation());
        holder.itemBinding.tchNative.setText(list.get(position).getTchNative());
        holder.itemBinding.tchTel.setText(list.get(position).getTchTel());
        holder.itemBinding.tchEmail.setText(list.get(position).getTchEmail());
        if (woman.equals(list.get(position).getTchSex())){
            holder.itemBinding.tchImag.setImageResource(R.mipmap.woman_teacher);
        }else {
            holder.itemBinding.tchImag.setImageResource(R.mipmap.man_teacher);
        }

        holder.itemBinding.cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.itemBinding.cardView1.setCardBackgroundColor(0xFFB8C0EE);
                if (courseItemBinding != null && list.get(position).getTchId() != list.get(index).getTchId()){
                    courseItemBinding.cardView1.setCardBackgroundColor(0xFFFFFFFF);
                }
                courseItemBinding = holder.itemBinding;
                index = position;
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Holder extends RecyclerView.ViewHolder {
        private CourseTchItemBinding itemBinding;

        public Holder(@NonNull View itemView) {
            super(itemView);
            itemBinding = CourseTchItemBinding.bind(itemView);
        }
    }
}
